using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

public class ExpUpgradeItem
{
    public ExpUpgradeItem(bool isWeapon)
    {
        IsWeapon = isWeapon;
    }
    public bool IsWeapon = false;    // 是否是武器
    public bool UpgradeFinish => CurrentLevel.Value >= MaxLevel;   // 判断是否已经升级完毕
    public string Key { get; private set;}    // 用来存储的Key
    public string Description => mDescriptionFactory(CurrentLevel.Value + 1);   // 描述
    public int MaxLevel { get; private set;}    // 最大等级
    public BindableProperty<int> CurrentLevel = new BindableProperty<int>(0);    // 当前等级
    public BindableProperty<bool> Visible = new BindableProperty<bool>();
    public string PairedName { get; private set;}
    public string PairedDescription { get; private set;}
    public string PairedIconName { get; private set;}
    public string IconName { get; private set;}
    public string Name { get; private set;}

    public void Upgrade()    // 升级的方法
    {
        CurrentLevel.Value++;
        mOnUpgrade?.Invoke(this, CurrentLevel.Value); // 调用外部配置的方法
        
        ExpUpgradeSystem.CheckAllUnlockedFinish(); // 检查所有的升级项是否都已经解锁
    }

    private Action<ExpUpgradeItem, int> mOnUpgrade;    // 对外配置的方法
    private Func<ExpUpgradeItem, bool> mCondition;
    private Func<int, string> mDescriptionFactory;

    /// <summary>
    /// 链式的封装
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public ExpUpgradeItem WithKey(string key)
    {
        Key = key;
        return this;    // 返回自身
    }

    public ExpUpgradeItem WithName(string name)
    {
        Name = name;
        return this;
    }

    public ExpUpgradeItem WithIconName(string iconName)
    {
        IconName = iconName;
        return this;
    }

    public ExpUpgradeItem WithPairedName(string pairedName)
    {
        PairedName = pairedName;
        return this;
    }

    public ExpUpgradeItem WithPairedIconName(string pairedIconName)
    {
        PairedIconName = pairedIconName;
        return this;
    }

    public ExpUpgradeItem WithPairedDescription(string pairedDescription)
    {
        PairedDescription = pairedDescription;
        return this;
    }

    public ExpUpgradeItem WithDescription(Func<int, string> descriptionFactory)
    {
        mDescriptionFactory = descriptionFactory;
        return this;
    }

    public ExpUpgradeItem OnUpgrade(Action<ExpUpgradeItem, int> onUpgrade)
    {
        mOnUpgrade = onUpgrade;
        return this;
    }

    public ExpUpgradeItem Condition(Func<ExpUpgradeItem, bool> condition)
    {
        mCondition = condition;
        return this;
    }

    public ExpUpgradeItem WithMaxLevel(int maxLevel)
    {
        MaxLevel = maxLevel;
        return this;
    }
}
